﻿
using A.M.Storage.Application;
using L.M.Review.Application.IServices;
using L.M.Review.Domain.DTO;
using L.M.Review.Domain.Entity;
using L.M.Review.Infrastruecure.IRepository;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L.M.Review.Application.Services
{
    public class Approval_formServices : BaseServices<approval_form, string>, IServices.IApproval_formServices
    {
        private readonly IApproval_formRepository approval_FormRepository;
        private readonly IRecord_tableServices record_TableServices;
        private readonly IProcess_nodesServices process_nodesServices;
        public Approval_formServices(IApproval_formRepository approval_FormRepository, IRecord_tableServices record_TableServices, IProcess_nodesServices process_nodesServices) : base(approval_FormRepository)
        {
            this.approval_FormRepository = approval_FormRepository;
            this.record_TableServices = record_TableServices;
            this.process_nodesServices = process_nodesServices;
        }

        /// <summary>
        /// 显示所有的审批单
        /// </summary>
        /// <returns>显示所有的审批单</returns>
        public async Task<List<ApprpvalDTO>> GetappList()
        {
            //先获取所有的审批系统
            var SsList = await approval_FormRepository.GetShowInforAsync();
            List<ApprpvalDTO> apprpvalDTOs = new List<ApprpvalDTO>();
            foreach (var item in SsList)
            {
                ApprpvalDTO apprpvalDTO = new ApprpvalDTO();
                apprpvalDTO.approval_number = item.approval_number;
                apprpvalDTO.approval_name = item.approval_name;
                apprpvalDTO.approval_on_no = item.approval_on_no;
                apprpvalDTOs.Add(apprpvalDTO);
            }
            //给二级赋值

            //获取所有的审批记录表
            var reslist = await record_TableServices.GetShowInforAsync();
            //找出审批节点中的第一级
            List<process_nodes> process_Nodes = new List<process_nodes>();
            foreach (var item in reslist)
            {
                var res = await process_nodesServices.GetShowObjAsync(c => c.process_nodes_number == item.process_nodes_number);
                process_Nodes.Add(res);
            }
            //添加子集
            //获取所有的审批数据
            var sList = await process_nodesServices.GetShowInforAsync();
            List<ApprpvalTwoDTO> apprpvalTwoDTOs1 = new List<ApprpvalTwoDTO>();
            var res1 = "";
            foreach (var item in process_Nodes)
            {
                if(item != null)
                {
                    if (res1 != item.passe_system_number)
                    {
                        apprpvalTwoDTOs1 = new List<ApprpvalTwoDTO>();
                    }
                    res1 = item.passe_system_number;
                    ApprpvalTwoDTO apprpvalTwoDTOs = new ApprpvalTwoDTO();
                    foreach (var item1 in apprpvalDTOs)
                    {
                        apprpvalTwoDTOs = await GteDiguiInfor(sList, item.process_nodes_number, item1.approval_number);
                        if (apprpvalTwoDTOs != null)
                        {
                            apprpvalTwoDTOs1.Add(apprpvalTwoDTOs);
                            item1.apprpvalTwoDTO = (apprpvalTwoDTOs1);
                        }
                    }
                }
                

            }
            return apprpvalDTOs;
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="sList"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApprpvalTwoDTO> GteDiguiInfor(List<process_nodes> sList, string? sId, string? sysId)
        {
            List<process_nodes> process_Nodes = new List<process_nodes>();
            var list = sList.Where(c => c.passe_system_number == sysId && c.process_nodes_number == sId).FirstOrDefault();
            if (list != null)
            {
                process_Nodes.Add(list);
            }
            while (list != null)
            {
                if (list == null)
                {
                    break;
                }
                var gets = process_Nodes[process_Nodes.Count() - 1];
                var res = sList.Where(c => c.upper_node == gets.lower_node).FirstOrDefault();
                if (res != null)
                {
                    process_Nodes.Add(res);
                }
                else
                {
                    break;
                }
            }
            ApprpvalTwoDTO apprpvalTwoDTO1 = new ApprpvalTwoDTO();
            if (process_Nodes.Count() != 0)
            {
                for (int i = 0; i < process_Nodes.Count(); i++)
                {
                    apprpvalTwoDTO1.process_nodes_number = process_Nodes[0].process_nodes_number;
                    apprpvalTwoDTO1.process_nodes_name = process_Nodes[1].process_nodes_name;
                    apprpvalTwoDTO1.process_nodes_role_name += process_Nodes[i].process_nodes_name + "→";
                    apprpvalTwoDTO1.Cdate = process_Nodes[i].process_date;
                    apprpvalTwoDTO1.ShujId = process_Nodes[i].passe_system_number;
                }
                if (apprpvalTwoDTO1.process_nodes_role_name != null)
                {
                    apprpvalTwoDTO1.process_nodes_role_name = apprpvalTwoDTO1.process_nodes_role_name.TrimEnd('→');
                }
                return apprpvalTwoDTO1;
            }
            else
            {
                return null;
            }


        }
    }
}
